define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'tongcheng/subscribe_message/index' + location.search,
                    add_url: 'tongcheng/subscribe_message/add',
                    // edit_url: 'tongcheng/subscribe_message/edit',
                    del_url: 'tongcheng/subscribe_message/del',
                    multi_url: 'tongcheng/subscribe_message/multi',
                    import_url: 'tongcheng/subscribe_message/import',
                    table: 'tongcheng_subscribe_message',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',

                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id')},
                        {field: 'client', title: __('Client'), searchList: {"CUSTOMER":__('CUSTOMER'),"RIDER":__('RIDER')}, formatter: Table.api.formatter.normal},
                        {field: 'name', title: __('Name'), operate: 'LIKE'},
                        {field: 'fields', title: __('Fields'), operate: 'LIKE'},
                        {field: 'tid', title: __('Tid')},
                        {field: 'template_id', title: __('Templete_id'), operate: 'LIKE'},
                        {field: 'createtime', title: __('Createtime'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                        {field: 'updatetime', title: __('Updatetime'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                        {field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate, formatter: Table.api.formatter.operate}
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);

            // 初始化订阅消息
            $('.btn-init').click(function () {
              Fast.api.ajax({
                  url: 'tongcheng/subscribe_message/init',
                  loading: true,
                  confirm: '确定要初始化订阅消息吗（会覆盖原有的订阅消息）？',
                  callback: function (data) {
                      console.log('data', data);
                      // Table.api.reload();
                      $('.btn-refresh').click();
                  }
              }, function (data, ret) {
                  $('.btn-refresh').click();
              });
            });
        },
        add: function () {
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            }
        }
    };
    return Controller;
});
